package com.lfg.message.delay.message.demo.delay.redis;

import org.redisson.api.RDelayedQueue;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author lfg
 * @version v1.0
 */
@RestController
public class Product3 {

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 频率为10s/20s/30s
     */
    private static final Long[] PUSH_DELAY = {10L, 20L, 30L};

    @RequestMapping("/product/message3")
    public String sendMessage() {
        for (Long aLong : PUSH_DELAY) {
            //推送到队列
            RQueue<String> queue = redissonClient.getBlockingQueue("redis-key-name");
            RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(queue);
            delayedQueue.offer("测试消息-Test Message-" + aLong, aLong, TimeUnit.SECONDS);
            delayedQueue.destroy();
        }
        return "已发送延时消息!";
    }

}
